package com.slwish.xty.ui.activity;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.slwish.common.base.ToolbarActivity;
import com.slwish.xty.R;
import com.slwish.xty.base.BaseApplication;
import com.slwish.xty.utils.LocaServiceUtils;

import butterknife.BindView;

/**
 * 百度地图
 */
public class MapActivity extends ToolbarActivity {
    @BindView(R.id.mapview)
    MapView mMapView;

    private BaiduMap mBaiduMap;
    private MyLocationData locData;
    private int mCurrentDirection = 0;
//    private double mCurrentLat = 0.0;//纬度
//    private double mCurrentLon = 0.0;//经度
    boolean isFirstLoc = true; // 是否首次定位
    private LocationClient mLocClient;
//    private BitmapDescriptor mCurrentMarker;

    @Override
    protected int getLayoutId() {
        return R.layout.act_map;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.map_title);
        initBaiduMap();
    }

    private void initBaiduMap() {
        mBaiduMap = mMapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        LocaServiceUtils locaServiceUtils = BaseApplication.getLocaService();
        locaServiceUtils.registerListener(new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(final BDLocation location) {
                // map view 销毁后不在处理新接收的位置
                if (location == null || mMapView == null) {
                    return;
                }

//                mCurrentLat = location.getLatitude();
//                mCurrentLon = location.getLongitude();
//                mCurrentAccracy = location.getRadius();
                locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                        // 此处设置开发者获取到的方向信息，顺时针0-360
                        .direction(mCurrentDirection).latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();
                mBaiduMap.setMyLocationData(locData);
                if (isFirstLoc) {
                    isFirstLoc = false;
                    LatLng ll = new LatLng(location.getLatitude(),
                            location.getLongitude());
                    MapStatus.Builder builder = new MapStatus.Builder();
                    builder.target(ll).zoom(19.0f);
                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                }

            }
        });
        locaServiceUtils.start();
        mLocClient = locaServiceUtils.getLocationClient();
        // 修改为自定义marker
//        mCurrentMarker = BitmapDescriptorFactory
//                .fromResource(R.mipmap.dtdwss);
//        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
//                MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker));

    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onStop() {
        //取消注册传感器监听
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // 退出时销毁定位
        if (mLocClient != null)
            mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }
}
